home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9392 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.6 KB

  1. Path: news.chevron.com!news
  2. From: "W.R.Volz" <hwrvo@chevron.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: how slow are virtual functions?
  5. Date: Fri, 01 Mar 1996 09:03:19 -0600
  6. Organization: Chevron Petroleum Technology Company
  7. Message-ID: <313711B7.41C67EA6@chevron.com>
  8. References: <4gioqc$9f0@onlink3.onlink.net> <31344ACF.1ABB@bbn.hp.com>
  9. NNTP-Posting-Host: hsun11.hou281.chevron.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Manfred Lange wrote:
  16. > wfss16@onlink.net wrote:
  17. > >
  18. > > Can anyone give me some timings/good anology as to how much slower calling a virtual
  19. > > function is as to a normal one?
  20. > >
  21. > > Thanks for your help!
  22.  
  23. I don't think what follows is a correct explanation. I don't think
  24. that any lookup is required, just a table access. Let me explain
  25. how I was told virtual functions are implemented:
  26.  
  27. Assume that you have a base class A with two virtual functions,
  28. AA and AB. A new class is derived from A, called X with virtual
  29. functions, XX and XY. X also overrides the virtual function
  30. AA from the inherited A class. I every class that has virtual
  31. functions, there is a hidden pointer to what is called the vtable
  32. (or virtual table). Note that if the class has functions that are
  33. not virtual, no lookup is required, since the called function
  34. address is known at link time. The vtable is set up for class as 
  35. follows:
  36.  
  37. Class A:
  38. Data:
  39. Vtable -> address of AA
  40.           address of AB.
  41.  
  42. For class X:
  43. Data:
  44. vtable -> address of AA overridden in X
  45.           address of AB
  46.           address of XA
  47.           address of XB.
  48.  
  49. Note that the offset in the vtable is the same for the same
  50. virtual function in different classes. Note that AB in class
  51. X was not overridden, so it points to the Ab member in
  52. class A, just as it should.
  53.  
  54. When a call to a virtaul function is made, the system loads
  55. the address of the vtable for the appropriate class and then from
  56. a fixed offset loads the address of the function and then
  57. calls the function. Hence if you call AA from class A, the vtable
  58. will be loaded for class A and the first address will be loaded
  59. and then called. If the real class is X, the table is still loaded
  60. but from class X this time, and the first entry is loaded and called.
  61. Note that there is only one copy of the vtable for each class
  62. while each instance of the classes has a single pointer to its vtable.
  63.  
  64. The net result is that there is a very small runtime penalty for
  65. virtual functions that consists of a few load instructions. The
  66. overhead is constant for any virtual function and there is not
  67. lookup involved (that has already been done in the compiler).
  68. Note that in many cases, there are several calls to different
  69. functions in the class, so the vtable may alreay be loaded
  70. which means that the overhead is a single load instruction,
  71. after the first load of th vtable. This all is optimized by
  72. the compiler.
  73.  
  74. I most likely have something wrong here, but it is probably of
  75. a minor nature so please excure me and I stand corrected
  76. in advance.
  77. > You will have a runtime overhead, that is caused by the fact the address of a virtual
  78. > function is determined during runtime. Each class has a address table (vtable) which
  79. > also contains the address of all virtual functions declared and defined for that
  80. > class. The runtime system has to look up the correct address depending on the type of
  81. > the object (the class the object is an instance of) and then calls the function. This
  82. > mechanism is also known as late binding.
  83. > For non-virtual functions, the binding can be done during link-time. This means that
  84. > no additional code has to be executed by the runtime-environment to determine the
  85. > address of the function to be executed.
  86. > So you will have a runtime overhead. But keep in mind, that the percentage of the
  87. > overhead in general is very small, as the code of the virtual function needs more time
  88. > to execute than the code for the overhead in most cases. Also the address-lookup is
  89. > implemented using hash-functions in most cases.
  90. > In the projects I was and still am working on, I never had the problem that calling
  91. > virtual functions needs too much processor time. (The first project was 300,000 lines
  92. > of code, 300 classes, took 2 years, the current project will have 200 classes, will
  93. > take approx. 6 months).
  94. > You can save more time by avoiding passing objects on the stack to the function. Use
  95. > pointers or references instead.
  96. > Hope this helps.
  97. > --
  98. > Regards, Manfred.
  99.  
  100. -- 
  101.  
  102. ======================
  103. Bill Volz
  104. Chevron Petroleum Technology Co.
  105. Earth Model/Interpretation & Analysis Division.
  106. P.O. Box 42832, Houston, TX, 77242-2832
  107. Phone: (713) 596-2059 Fax: (713) 596-3009
  108.